﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dominio.Logger;
using System.Reflection;
using Dominio.Excepciones;
using System.IO;

namespace Dominio.Logica
{
    public class ManejadorLog
    {
        private static ManejadorLog manejadorLog;

        private static ILog Log;     

        private ManejadorLog() { }

        public static ManejadorLog GetLog()
        {
            if (manejadorLog == null)
            {
                manejadorLog = new ManejadorLog();
                CargarDllLog();
            }
            return manejadorLog;
        }

        public static void CargarDllLog()
        {
            string[] TipoLog = System.Configuration.ConfigurationManager.AppSettings.GetValues("TipoLog");
            string[] DllLog = System.Configuration.ConfigurationManager.AppSettings.GetValues("DllLog");
            try
            {
                Assembly assembly = Assembly.LoadFrom(DllLog[0]);
                Type type = assembly.GetType("Logging." + TipoLog[0]);
                Log = (ILog)Activator.CreateInstance(type);
                if (Log == null)
                {
                    throw new NoExistingLogException();
                }
            }
            catch (ArgumentNullException)
            {
                throw new NoExistingLogException();
            }
            catch (FileNotFoundException ex)
            {
                throw ex;
            }
        }

        public void Grabar(string texto)
        {
            Log.Grabar(texto);
        }

        public string RecuperarLog(string ruta)
        {
            return Log.RecuperarLog(ruta);
        }


    }
}
